﻿using System;
using System.Collections.Generic;
using System.Data;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ProjectSunny.Admin.Controls
{
    public partial class Category : System.Web.UI.UserControl
    {
        public int count = 1;
        protected void Page_Load(object sender, EventArgs e)
        {
          
        }
        protected void Cate1_SelectedIndexChanged(object sender, EventArgs e)
        {
            count = 1;
            // Chon danh muc cap 1 load danh muc cap 2
            Cate2.Items.Clear();
            Cate3.Items.Clear();

            int index = Cate1.SelectedIndex;
            if (index > 0)
            {
                int id = Convert.ToInt32(Cate1.SelectedValue);
                // Load du lieu
                DataTable dt = DAL.DCollector.DBCollector.Instance.StoredSelectTableWhereID("tbl_Category","Category_ID2", id.ToString());
                ListItem lt = new ListItem("Chon danh muc", "-1");
                Cate2.Items.Add(lt);
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    lt = new ListItem(dt.Rows[i]["Category_Name"].ToString(), dt.Rows[i]["Category_ID1"].ToString());
                    Cate2.Items.Add(lt);
                }
                rpt_Sanpham.DataSource = dt;
                rpt_Sanpham.DataBind();
            }
        }

        protected void Cate2_SelectedIndexChanged(object sender, EventArgs e)
        {
            count = 1;
            // Chon danh muc cap 1 load danh muc cap 2
            Cate3.Items.Clear();
            int index = Cate2.SelectedIndex;
            if (index > 0)
            {
                int id = Convert.ToInt32(Cate2.SelectedValue);
                // Load du lieu
                DataTable dt = DAL.DCollector.DBCollector.Instance.StoredSelectTableWhereID("tbl_Category","Category_ID2", id.ToString());
                ListItem lt = new ListItem("Chon danh muc", "-1");
                Cate3.Items.Add(lt);
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    lt = new ListItem(dt.Rows[i]["Category_Name"].ToString(), dt.Rows[i]["Category_ID1"].ToString());
                    Cate3.Items.Add(lt);
                }
                rpt_Sanpham.DataSource = dt;
                rpt_Sanpham.DataBind();
            }

        }

        protected void Cate3_SelectedIndexChanged(object sender, EventArgs e)
        {
        }

        protected void root_Click(object sender, EventArgs e)
        {
            // Them du lieu vao cat 1
            DataTable dt = DAL.DCollector.DBCollector.Instance.StoredSelectTableWhereID("tbl_Category","Category_ID2", "0");
            ListItem lt = new ListItem("Chon danh muc", "-1");
            Cate1.Items.Add(lt);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                lt = new ListItem(dt.Rows[i]["Category_Name"].ToString(), dt.Rows[i]["Category_ID1"].ToString());
                Cate1.Items.Add(lt);
            }
            Cate1.SelectedIndex = 0;
            rpt_Sanpham.DataSource = dt;
            rpt_Sanpham.DataBind();
            count = 1;
        }

        protected void rpt_Sanpham_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "delete")
            {
                // Xoa
                int id = Convert.ToInt32(e.CommandArgument.ToString());
                DAL.DCollector.DBCollector.Instance.StoredDelete("tbl_Category", "Category_ID1", id.ToString());
                Response.Redirect("/Admin/Index.aspx?panel=2&sub=1");
            }
        }
    }
}